<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-sub-content">
  <div class="gv-forms gv-forms-fluid" layout="column">
    <h1>Users</h1>

    <div class="gv-form">
      <div class="apis-header-filter">
        <form flex layout="row" layout-align="center center" name="formSearch" autocomplete="off" ng-submit="$ctrl.search()">
          <input
            class="form-control apis-filter"
            type="text"
            style="max-width: 960px"
            ng-model="$ctrl.query"
            placeholder="Search users"
            autofocus
          />
          <md-button class="md-primary gv-search-button" type="submit" aria-label="Search">
            <ng-md-icon icon="search"></ng-md-icon>
          </md-button>
        </form>
      </div>
      <h2></h2>
      <div class="gv-form-content" layout="column">
        <md-table-container>
          <table md-table class="gv-table-dense">
            <thead md-head>
              <tr md-row>
                <th md-column width="1%" nowrap></th>
                <th md-column>Display name</th>
                <th md-column width="15%" nowrap>Status</th>
                <th md-column width="15%" nowrap>Email</th>
                <th md-column width="15%" nowrap>Source</th>
                <th md-column width="1%" nowrap></th>
              </tr>
            </thead>
            <tbody md-body>
              <tr md-row ng-repeat="user in $ctrl.usersPage.data | orderBy: orderUser">
                <td md-cell>
                  <gravitee-identity-picture
                    image-id="user.id"
                    image-url="$ctrl.getUserPicture(user)"
                    image-name="user.displayName"
                    image-width="32"
                    image-border-radius="true"
                  ></gravitee-identity-picture>
                </td>
                <td md-cell>
                  <a ui-sref="organization.settings.ajs-user({userId: user.id})">
                    {{user.displayName}}
                    <span ng-if="user.primary_owner" flex class="badge" style="background-color: #039be5">Primary Owner</span>
                    <span ng-if="user.number_of_active_tokens > 0" flex class="badge" style="background-color: #e53935">
                      Active Token{{user.number_of_active_tokens > 1 ? 's' : ''}}
                    </span>
                  </a>
                </td>
                <td md-cell width="15%" nowrap>
                  <span ng-if="user.status === 'ACTIVE'" style="color: green">{{user.status}}</span>
                  <span ng-if="user.status === 'PENDING'" style="color: orange">{{user.status}}</span>
                  <span ng-if="user.status === 'REJECTED'" style="color: red">{{user.status}}</span>
                </td>
                <td md-cell width="15%" nowrap>{{user.email}}</td>
                <td md-cell width="15%" nowrap>{{user.source}}</td>
                <td md-cell width="1%" nowrap ng-click="$event.stopPropagation()">
                  <span ng-if="!user.primary_owner" permission permission-only="['organization-user-d']">
                    <md-tooltip md-direction="top">delete</md-tooltip>
                    <ng-md-icon
                      icon="delete"
                      style="width: 20px; display: block"
                      ng-click="$ctrl.remove($event, user)"
                      aria-label="delete-user"
                    ></ng-md-icon>
                  </span>
                </td>
              </tr>
            </tbody>
          </table>
        </md-table-container>
        <md-table-pagination
          ng-if="$ctrl.usersPage.page.size > 0"
          md-limit="$ctrl.usersPage.page.per_page"
          md-page="$ctrl.usersPage.page.current"
          md-total="{{$ctrl.usersPage.page.total_elements}}"
          md-on-paginate="$ctrl.onPaginate"
        >
        </md-table-pagination>
      </div>
    </div>
  </div>

  <md-button
    ng-click="$ctrl.newUser()"
    aria-label="Add user"
    class="md-fab md-fab-bottom-right md-fab-scrollable"
    ng-class="{'gv-help-displayed': $ctrl.$rootScope.helpDisplayed}"
  >
    <md-tooltip md-direction="top" md-visible="false">Add user</md-tooltip>
    <ng-md-icon icon="add"></ng-md-icon>
  </md-button>
</div>
